﻿namespace DB2WORD
{
    public partial class FrmAiResult : Form
    {
        public FrmAiResult()
        {
            InitializeComponent();
        }

        private static int widthValue = 72;
        private static int heightValue = 170;
        public void SetContent(Dictionary<string, string> values)
        {
            var kvps = values.ToList();
            var col = 1;
            var row = kvps.Count;
            while(true)
            {
                if(row <= 40)
                    break;
                col++;
                row = kvps.Count / col;
                if(kvps.Count % col > 0)
                {
                    row++;
                }
            }

            var labels = new List<Label>();
            for(int c = 0; c < col; c++)
            {
                labels.Add(new Label { TextAlign = ContentAlignment.TopRight });
                labels.Add(new Label());
                labels.Add(new Label());
                for(int r = 0; r < row; r++)
                {
                    if(row * c + r < kvps.Count)
                    {
                        var v = kvps[row * c + r];
                        labels[c * 3].Text += v.Key + "\r\n";
                        labels[c * 3 + 1].Text += "->\r\n";
                        labels[c * 3 + 2].Text += v.Value + "\r\n";
                    }
                }
            }

            var gp = this.label1.CreateGraphics();
            var font = this.label1.Font;
            var left = label1.Left + 20;
            var lblheight = 0;
            for(int i = 0; i < labels.Count; i++)
            {
                var lbl = labels[i];
                lbl.Text = lbl.Text.TrimEnd();
                lbl.AutoSize = true;
                lbl.Left = left;
                lbl.Top = label1.Top + 25;
                var size = gp.MeasureString(lbl.Text, font);
                left += (int)(size.Width * 1.2) + 10;
                if((i + 1) % 3 == 0)
                {
                    left += 20;
                }
                this.Controls.Add(lbl);
                if(lblheight == 0)
                {
                    lblheight = (int)(size.Height * 1.2) + 1;
                }
            }

            this.btnOK.BringToFront();
            this.btnCancel.BringToFront();
            var width = left + widthValue;
            var height = lblheight + heightValue;
            if(this.Width < width)
            {
                this.Width = width;
            }
            if(this.Height < height)
            {
                this.Height = height;
            }
        }
    }
}
